package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ZhangXiXi
 * @ClassName CheckItemController
 * @Description 检查项控制器
 * @date 2021/1/5 15:28
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/checkitem")
@RestController
public class CheckItemController {
    @DubboReference
    private CheckItemService checkItemService;

    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        log.info("[检查项:添加]data:{}", checkItem);
        //调用service插入数据
        checkItemService.add(checkItem);
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查项:分页查询]data:{}", queryPageBean);
        //调用service插入数据
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @PostMapping("/delete")
    public Result delete(Integer id) {
        log.info("[检查项:删除]id:{}", id);
        //调用service插入数据
        checkItemService.deleteById(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        log.info("[检查项:更新]data:{}", checkItem);
        //调用service更新数据
        checkItemService.edit(checkItem);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        //调用service更新数据
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        log.info("[检查项-查询所有]~");
        List<CheckItem> checkItems = checkItemService.findAll();
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItems);
    }
}
